import java.util.ArrayList;
import java.util.Scanner;

/*
 * Bob Dayton
 * COSC - 716 Assignment 2
 * The IngredientMenuIterator returns only 
 * items that have the same main ingredient as
 * specified by the user
 */


public class IngredientMenuIterator implements MenuIterator{
	
	//private MenuItem[] items;
	private ArrayList<MenuItem> items = new ArrayList<MenuItem>();
	private int count;
	private String ingredient;
	private int current; 
	
	Scanner in = new Scanner(System.in);
	
	public IngredientMenuIterator(ArrayList<MenuItem> i, String ing)
	{
		items = i;
		count = 0;
		//System.out.println("Enter main ingredient:");
		//ingredient = in.next();
		ingredient = ing;
	}

	@Override
	public MenuItem next() {
		setNext();
		return items.get(current);
	}
	
	/*
	 * hasNext() will check to see
	 * if there are more menu items in the array
	 * that have the specified main ingredient
	 */

	@Override
	public boolean hasNext() {
		boolean found = false;
		/*
		 * since we just want to know if there is a next item
		 * we will use a temporary count variable instead
		 * of updating our actual count variable 
		 */
		int tempCount = count;
		if(items.get(tempCount)!= null)		
		{
			
			while(!found && tempCount < items.size())
			{
				if(items.get(tempCount) != null)		
				{
					//can not compare strings with ==
					if(!items.get(tempCount).getMainIngredient().toLowerCase().equals(ingredient.toLowerCase()) )
					{
						tempCount++;
					}
					else
					{
						tempCount++;
						found = true;
					}
				}
			}
		}
		return found;
		
	}
	
	/*
	 * setNext() sets the next item for the iterator to return
	 */
	private void setNext()
	{
		boolean found = false;		
		if(items.get(count)!= null)		
		{
			while(!found && count < items.size())
			{
				if(!items.get(count).getMainIngredient().toLowerCase().equals(ingredient.toLowerCase()) )
				{
					count++;
				}
				else
				{
					current = count;
					count++;
					found = true;
				}
			}
		}
		
		
	}

}
